package cn.tedu.boke.portal.controller;

import cn.tedu.boke.portal.model.User;
import cn.tedu.boke.portal.service.IUserService;
import cn.tedu.boke.portal.vo.RegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Xdoor
 * @version 1.0
 * @date 2021/10/29 15:16
 */

@RestController
//lombok提供的一个记录日志的注解
//一旦在类上添加@Slfj,这个类的方法中就可以使用log对象记录日志
@Slf4j
public class SystemController
{
    @Autowired
    private IUserService userService;

    @PostMapping("/reg")
    public String register(@Validated RegisterVo registerVo, BindingResult result)
    {
        log.debug("接收到用户信息:{}", registerVo);
        if (result.hasErrors())
        {
            String msg = result.getFieldError().getDefaultMessage();
            return msg;
        }
        userService.register(registerVo);
        return "ok";
    }

    @GetMapping("/checkLogin")
    public User checkLogin(@AuthenticationPrincipal UserDetails user)
    {
        User u = userService.findUserByUsername(user.getUsername());
        return u;
    }


}
